<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handlecreateForm({})" v-auth="'btn:add:aqcsf'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起申请') }}
        </a-button>
        <template v-if="props.payType == '222'">
          <a-button type="default" @click="handleExport()">
            <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
          </a-button>
          <a-button type="default" @click="handleImport()">
            <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
          </a-button>
        </template>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno })">
          {{ record.projectname }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <createForm @register="registercreateDrawer" @success="handleSuccess" />
    <accountForm @register="registeraccountDrawer" @success="handleSuccess" />
    <resultForm @register="registerresultDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsPayPmPayrequestList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmPayrequestDelete, pmPayrequestListData } from '/@/api/pay/pmPayrequest';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import createForm from './createForm.vue';
  import accountForm from './accountForm.vue';
  import resultForm from './resultForm.vue';
  import FormImport from './formImport.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('pay.pmPayrequest');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('付款申请管理'),
  };

  const props = defineProps({
    payType: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('单据编号'),
        field: 'djno',
        component: 'Input',
      },
      {
        label: t('项目编号'),
        field: 'projectno',
        component: 'Input',
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      // {
      //   label: t('款项类型'),
      //   field: 'payType',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_pay_type',
      //     allowClear: true,
      //   },
      // },
      {
        label: t('申请人'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('审批状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('单据编号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 230,
      align: 'left',
      // fixed:'left',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 130,
      align: 'left',
      // fixed:'left',
    },
    {
      title: t('款项类型'),
      dataIndex: 'payType',
      key: 'a.pay_type',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'zf_pay_type',
    },
    {
      title: t('结算金额'),
      dataIndex: 'imoney',
      key: 'a.imoney',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('申请说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('申请人编码'),
      dataIndex: 'createBy',
      key: 'a.create_by',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('申请人名称'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('实际金额'),
      dataIndex: 'realMoney',
      key: 'a.real_money',
      sorter: true,
      width: 100,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('核价金额'),
      dataIndex: 'accountMoney',
      key: 'a.account_money',
      sorter: true,
      width: 100,
      align: 'center',
      //ifShow: props.payType == '222' ? true : false,
      ifShow: false,
    },
    {
      title: t('审批状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 120,
    align: 'left',
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑付款申请'),
        onClick: handlecreateForm.bind(this, { djno: record.djno }),
        auth: 'btn:edit:aqcsf',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除付款申请'),
        popConfirm: {
          title: t('是否确认删除付款申请'),
          confirm: handleDelete.bind(this, { djno: record.djno }),
        },
        auth: 'btn:delete:aqcsf',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registeraccountDrawer, { openDrawer: openaccountDrawer }] = useDrawer();
  const [registerresultDrawer, { openDrawer: openresultDrawer }] = useDrawer();
  const [registercreateDrawer, { openDrawer: opencreateDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmPayrequestListData,
    beforeFetch: (params) => {
      params.payType = params.payType ? params.payType : props.payType;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleresultForm(record: Recordable) {
    if (props.payType) {
      record.payType = props.payType;
    }
    openresultDrawer(true, record);
  }
  function handlecreateForm(record: Recordable) {
    if (props.payType) {
      record.payType = props.payType;
    }
    opencreateDrawer(true, record);
  }
  function handleForm(record: Recordable) {
    if (props.payType) {
      record.payType = props.payType;
    }
    openDrawer(true, record);
  }
  function handleaccountForm(record: Recordable) {
    if (props.payType) {
      record.payType = props.payType;
    }
    openaccountDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmPayrequestDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'Flow_PmPayRequest', bizKey: record.id });
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/pay/pmPayrequest/exportData',
      //target: '_self',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  onMounted(async () => {
    console.log(props.payType, 'payType===');
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
  }
</script>
